/**
 * Created by dong on 15/10/30.
 */

'use strict';

var log4js =require('log4js');


//log4js中的日志级别.
let _globalLogLv =30000 ;
let _globalLogLvStr ="WARN";



let _logSingleService = {};
let _loggerMap ={};
/**
 *  获取日志级别..
 *
 * @return {*}
 * fatal:50000
 * error:40000
 * warn 30000
 * info d
 * debug 10000
 * trace 5000
 *
 *
 */
_logSingleService.getLogLv = ()=>{
    return _globalLogLv;
}

//
///**
// * 获取建站消费者常规日志
// */
//logService.getConsumerLogger =()=>{
//    return logService.getLogger('consumer');
//}
//
///**
// * 获取建站消费者打包相关日志
// */
//logService.getConsumerPackageLogger =()=>{
//    return logService.getLogger('consumer-package');
//}


/**
 *
 * @param cateory
 */
_logSingleService.getLogger = (cateory)=>{
    if(_loggerMap[cateory]){
        return _loggerMap[cateory];
    }else{
        _loggerMap[cateory]=_generateLogger(cateory);
        return _loggerMap[cateory];
    }
}


function _generateLogger(catrory) {
    var logger = log4js.getLogger(catrory);
    logger.setLevel(_globalLogLvStr);
    return logger;
}

/**
 *
 * @param log4jsConfig
 *
 * https://www.npmjs.com/package/log4js
 {
  "appenders": [{
      "type": "console"
    },{ "type": "file",
      "filename": "logs/x-site-d2c-render.log",
      "category": "xsite-d2c" ,
      "maxLogSize": 50000000,
      "backups": 10
    }],
  "logLevel":"DEBUG"
}
 * @return {{}}
 */
module.exports =(log4jsConfig) => {
    log4js.configure(log4jsConfig);

    _globalLogLvStr =log4jsConfig.logLevel;
    _globalLogLv=log4js.levels.toLevel(_globalLogLvStr,_globalLogLv);

    return _logSingleService;
}
